﻿using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

/*
 * 封装一个简单的托盘图片包装类（2014-9-6）
 * 
 * 1.显示托盘
 * 
 * 2.这边还有一个问题，就是要设置窗体的属性，ShowInTaskBar，这个不太好做
 *      这个问题已经解决了
 */

namespace Ycg.Winform
{
    public class NotifyIconWrapper
    {
        private Icon _icon = default(Icon);
        private NotifyIcon _notifyIcon = new NotifyIcon();
        private ContextMenu _contextMenu = new ContextMenu();

        public NotifyIconWrapper(string iconName)
        {
            if (string.IsNullOrEmpty(iconName))
            {
                throw new ArgumentNullException("iconName");
            }
            this._icon = new Icon(iconName);
            this._notifyIcon.Visible = true;
            this._notifyIcon.Icon = this._icon;
            this._notifyIcon.ContextMenu = this._contextMenu;

            Application.ApplicationExit += Application_ApplicationExit;
        }

        void Application_ApplicationExit(object sender, EventArgs e)
        {
            this._notifyIcon.Visible = false;
        }

        public IEnumerator GetMenuItems
        {
            get
            {
                return this._contextMenu.MenuItems.GetEnumerator();
            }
        }

        public void AddMenuItem(MenuItem menuItem)
        {
            if (menuItem == null)
            {
                throw new ArgumentNullException("menuItem");
            }
            this._contextMenu.MenuItems.Add(menuItem);
        }

        public void AddMenuItem(MenuItem menuItem, EventHandler eventHandler)
        {
            this.AddMenuItem(menuItem);
            menuItem.Click += eventHandler;
        }
    }
}
